package cn.edu.kzjsxy.graduationproject.controller;


import cn.edu.kzjsxy.graduationproject.common.Page;
import cn.edu.kzjsxy.graduationproject.common.Result;
import cn.edu.kzjsxy.graduationproject.dto.DormitoryDTO;
import cn.edu.kzjsxy.graduationproject.entity.Dormitories;
import cn.edu.kzjsxy.graduationproject.entity.StudentDormAssignments;
import cn.edu.kzjsxy.graduationproject.service.IBuildingsService;
import cn.edu.kzjsxy.graduationproject.service.IDormitoriesService;
import cn.edu.kzjsxy.graduationproject.service.IStudentDormAssignmentsService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 宿舍信息表 前端控制器
 * </p>
 *
 * @author chén jiā jùn
 * @since 2024-04-14
 */
@Api(tags = "宿舍信息表")
@RestController
@RequestMapping("/dormitories")
public class DormitoriesController {

// 宿舍信息表的service
	@Autowired
	private IDormitoriesService dormitoriesService;
//	 学生宿舍分配service
	@Autowired
	private IStudentDormAssignmentsService studentDormAssignmentsService;

	@GetMapping("/{dormitoryId}")
	@ApiOperation("根据宿舍ID查询宿舍信息")
	public Result<Dormitories> getDormitoriesById(@PathVariable String dormitoryId) {
		Dormitories dormitories = dormitoriesService.getById(dormitoryId);
		return Result.success(dormitories);
	}
	@GetMapping("/selectDormitoryDTOById/{dormitoryId}")
	@ApiOperation("根据宿舍ID查询宿舍信息")
	public Result<DormitoryDTO> selectDormitoryDTOById(@PathVariable String dormitoryId) {
		DormitoryDTO dormitoryDTO = dormitoriesService.selectDormitoryDTOById(dormitoryId);
		return Result.success(dormitoryDTO);
	}

	@ApiOperation("添加宿舍信息")
	@PostMapping("/add")
	public Result addDormitory(@RequestBody Dormitories dormitory) {
		boolean success = dormitoriesService.save(dormitory);
		return Result.judgment(success);
	}

	@ApiOperation("删除宿舍信息")
	@DeleteMapping("/delete/{dormitoryId}")
	public Result deleteDormitory(@PathVariable String dormitoryId) {
		List<StudentDormAssignments> studentDormAssignments = studentDormAssignmentsService.selectStudentDormAssignmentsByCondition(StudentDormAssignments.builder().dormitoryId(dormitoryId).build());
		if(studentDormAssignments==null||studentDormAssignments.isEmpty()){
			boolean success = dormitoriesService.removeById(dormitoryId);
			return Result.judgment(success);
		}
		return Result.error("当前宿舍存在学生不可以删除!");
	}

	@ApiOperation("更新宿舍信息")
	@PutMapping("/update")
	public Result updateDormitory(@RequestBody Dormitories dormitory) {
		boolean success = dormitoriesService.updateById(dormitory);
		return Result.judgment(success);
	}

	@ApiOperation("根据条件查询宿舍信息")
	@PostMapping("/list")
	public Result<List<Dormitories>> selectDormitoriesByCondition(@RequestBody Dormitories dormitory) {
		List<Dormitories> dormitoriesList = dormitoriesService.selectDormitoriesByCondition(dormitory);
		return Result.success(dormitoriesList);
	}

	@ApiOperation("分页获取宿舍信息")
	@PostMapping("/page")
	public Result<PageInfo<Dormitories>> selectDormitoriesByPage(Page page, @RequestBody Dormitories dormitory) {
		return dormitoriesService.selectDormitoriesByPage(page, dormitory);
	}

	@ApiOperation("分页获取宿舍信息(多表联查)")
	@PostMapping("/selectDormitoryDTOByPage")
	public Result<PageInfo<DormitoryDTO>> selectDormitoriesByPage(Page page, @RequestBody DormitoryDTO dormitoryDTO, HttpServletRequest request) {
		return dormitoriesService.selectDormitoryDTOByPage(page, dormitoryDTO,request);
	}

}
